package com.example.rabbitmq.example.rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Random;

public class Producer {

    private static final String[]  LEVEL = {"info", "error", "warn"};
    private static final String[]  AREA = {"beijing", "shanghai", "shenzhen"};
    private static final String[]  BIZ = {"edu","biz","emp"};
    private static Random random = new Random();

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new  ConnectionFactory();
        factory.setUri("amqp://admin:admin@192.168.1.3:5672/%2f");
        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();

        channel.exchangeDeclare("ex.topic", "topic", true, false, null);

        String area,level, biz, routeKey, message;
        for (int i = 0; i < 100; i++) {
            area = AREA[random.nextInt(AREA.length)];
            level = LEVEL[random.nextInt(LEVEL.length)];
            biz = BIZ[random.nextInt(BIZ.length)];
            message = area+"."+level+"."+ biz +"====="+i;
            channel.basicPublish("ex.topic", area+"."+level+"."+ biz, null, message.getBytes(StandardCharsets.UTF_8));
        }
        channel.close();
        connection.close();
    }
}
